/**
 * 
 */
package com.myBase.core.modules.goldcointraderecord.web;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.Gson;
import com.myBase.core.entity.GoldcoinTradeRecord;
import com.myBase.core.facade.service.GoldcoinActivitycashpoolServiceFacade;
import com.myBase.core.facade.service.GoldcoinTradeRecordServiceFacade;
import com.myBase.framework.common.config.Global;
import com.myBase.common.persistence.Page;
import com.myBase.framework.common.utils.StringUtils;
import com.myBase.framework.common.web.BaseController;

/**
 * 用户金币获取、消费、过期相关流水Controller
 * @author WuJie
 * @version 2017-05-04
 */
@Controller
@RequestMapping(value = "${adminPath}/goldcointraderecord/goldcoinTradeRecord")
public class GoldcoinTradeRecordController extends BaseController {

    @Autowired
    private GoldcoinTradeRecordServiceFacade goldcoinTradeRecordService;

    @Autowired
    private GoldcoinActivitycashpoolServiceFacade goldcoinActivitycashpoolService;

    @ModelAttribute
    public GoldcoinTradeRecord get(@RequestParam(required = false) String id) {
        GoldcoinTradeRecord entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = goldcoinTradeRecordService.get(id);
        }
        if (entity == null) {
            entity = new GoldcoinTradeRecord();
        }
        return entity;
    }

    @RequiresPermissions("goldcointraderecord:goldcoinTradeRecord:view")
    @RequestMapping(value = { "list", "" })
    public String list(GoldcoinTradeRecord goldcoinTradeRecord, HttpServletRequest request,
            HttpServletResponse response, Model model) {
        Page<GoldcoinTradeRecord> page = goldcoinTradeRecordService
                .findPage(new Page<GoldcoinTradeRecord>(request, response), goldcoinTradeRecord);
        model.addAttribute("page", page);
        return "modules/goldcointraderecord/goldcoinTradeRecordList";
    }

    @RequiresPermissions("goldcointraderecord:goldcoinTradeRecord:view")
    @RequestMapping(value = "form")
    public String form(GoldcoinTradeRecord goldcoinTradeRecord, Model model) {
        model.addAttribute("goldcoinTradeRecord", goldcoinTradeRecord);
        return "modules/goldcointraderecord/goldcoinTradeRecordForm";
    }

    @RequiresPermissions("goldcointraderecord:goldcoinTradeRecord:edit")
    @RequestMapping(value = "save")
    public String save(GoldcoinTradeRecord goldcoinTradeRecord, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, goldcoinTradeRecord)) {
            return form(goldcoinTradeRecord, model);
        }
        goldcoinTradeRecordService.save(goldcoinTradeRecord);
        addMessage(redirectAttributes, "保存用户金币流水成功");
        return "redirect:" + Global.getAdminPath() + "/goldcointraderecord/goldcoinTradeRecord/?repage";
    }

    @RequiresPermissions("goldcointraderecord:goldcoinTradeRecord:edit")
    @RequestMapping(value = "delete")
    public String delete(GoldcoinTradeRecord goldcoinTradeRecord, RedirectAttributes redirectAttributes) {
        goldcoinTradeRecordService.delete(goldcoinTradeRecord);
        addMessage(redirectAttributes, "删除用户金币流水成功");
        return "redirect:" + Global.getAdminPath() + "/goldcointraderecord/goldcoinTradeRecord/?repage";
    }

    /**
     * * userGoldGainList(用户金币领取查询) 
     * @param   GoldcoinTradeRecord
     * @return page
     * @Exception 异常对象    
     */
    @RequiresPermissions("goldcointraderecord:goldcoinTradeRecord:view")
    @RequestMapping(value = { "userGoldGainList", "" })
    public String userGoldGainList(GoldcoinTradeRecord goldcoinTradeRecord, HttpServletRequest request,
            HttpServletResponse response, Model model) {

        //平台编码
        String branchId = request.getParameter("branchId");
        logger.info("用户金币领取查询  Controller  开始,branchId=" + branchId);
        //获取查询条件活动id 页面回显
        String actId = goldcoinTradeRecord.getActivityId();
        Page<GoldcoinTradeRecord> page = null;
        List<HashMap<String, Object>> activity = null;
        goldcoinTradeRecord.setBranchid(branchId);
        try {
            //所有金币活动
            activity = goldcoinActivitycashpoolService.getALLGoldActivity(branchId);
            page = goldcoinTradeRecordService.findUserGoldGainList(new Page<GoldcoinTradeRecord>(request, response),
                    goldcoinTradeRecord);
            List<GoldcoinTradeRecord> list = page.getList();
            //循环用户领取记录集合  设置金币有效时间
            for (GoldcoinTradeRecord goldRecord : list) {
                if (goldRecord.getGoldValidityStyle().equals("02")) {
                    //金币有效期类型 为按季度计算的  金币有效期开始时间为金币领取时间
                    goldRecord.setGoldValidityStart(goldRecord.getCreateDate());
                } else if (goldRecord.getGoldValidityStyle().equals("03")) {
                    //金币有效期类型 为按天数计算  金币有效期开始时间为金币领取时间    结束时间 为领取时间加上有效天数
                    Calendar cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, goldRecord.getGoldValidityDay());
                    goldRecord.setGoldValidityStart(goldRecord.getCreateDate());
                    goldRecord.setGoldValidityEnd(cal.getTime());
                }

            }

        } catch (Exception e) {
            logger.error("用户金币领取查询  Controller 执行异常", e);
            model.addAttribute("message", "用户金币领取查询异常");
        }
        model.addAttribute("page", page);
        model.addAttribute("activity", activity);
        model.addAttribute("actId", actId);//活动id 回显
        model.addAttribute("branchId", branchId);
        return "modules/goldcointraderecord/goldUserGainList";
    }

    /**
     * * UserGoldOverdueList(用户金币过期查询) 
     * @param   GoldcoinTradeRecord
     * @return  page
     * @Exception 异常对象    
     */
    @RequiresPermissions("goldcointraderecord:goldcoinTradeRecord:view")
    @RequestMapping(value = { "userGoldOverdueList", "" })
    public String UserGoldOverdueList(GoldcoinTradeRecord goldcoinTradeRecord, HttpServletRequest request,
            HttpServletResponse response, Model model) {

        //平台编码
        String branchId = request.getParameter("branchId");
        logger.info("用户金币过期查询 controller 开始,branchId=" + branchId);
        goldcoinTradeRecord.setBranchid(branchId);
        Page<GoldcoinTradeRecord> page = null;
        List<HashMap<String, Object>> goldCount = null;
        try {
            page = goldcoinTradeRecordService.findUserGoldOverdueList(new Page<GoldcoinTradeRecord>(request, response),
                    goldcoinTradeRecord);
            //用户金币过期查询,查询总人数,金币总数量，根据设备分组数量
            goldCount = goldcoinTradeRecordService.getUserGoldCount(goldcoinTradeRecord);

            List<GoldcoinTradeRecord> list = page.getList();
            //循环用户领取记录集合  设置金币有效时间
            for (GoldcoinTradeRecord goldRecord : list) {
                if (goldRecord.getGoldValidityStyle().equals("01")) {
                    //金币有效期类型 为固定时间段     金币有效时间为金币有效开始时间-金币有效结束时间
                    Date date1 = goldRecord.getGoldValidityStart();
                    Date date2 = goldRecord.getGoldValidityEnd();
                    int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
                    goldRecord.setGoldValidityDay(days);
                } else if (goldRecord.getGoldValidityStyle().equals("02")) {
                    //金币有效期类型 为按季度计算有效期    金币有效期时间为金币领取时间 - 金币有效结束时间
                    Date date1 = goldRecord.getCreateDate();
                    Date date2 = goldRecord.getGoldValidityEnd();
                    int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
                    goldRecord.setGoldValidityDay(days);
                }

            }

        } catch (Exception e) {
            logger.error("用户金币过期查询  Controller 执行异常", e);
            model.addAttribute("message", "用户金币过期查询异常");
        }

        Gson gson = new Gson();
        String json = gson.toJson(goldCount);
        model.addAttribute("page", page);
        model.addAttribute("json", json);
        model.addAttribute("branchId", branchId);
        return "modules/goldcointraderecord/golduserOverdueList";

    }

}